MessageGrid = {};

MessageGrid.sound = true;//短信声音默认打开
MessageGrid.popup = true;//弹窗默认打开
MessageGrid.refreshInterval = 30;
MessageGrid.displayTime = null;
MessageGrid.autoRefresh = false;
MessageGrid.popNum = 0;
MessageGrid.showVehicleOnMap = false;
MessageGrid.maxPopupNum = 20;//最多弹窗的短信条数，超过后，前面的要清除避免占用内存

MessageGrid.create = function () {
	$('#messageWindow').window('close');//初始化时，隐藏短信弹屏窗口

	//this.refreshInterval = 10;
	this.timerName = "messageTimer";
	var strInterval = this.refreshInterval + 's';
	var me = this;

	$('body').everyTime(strInterval, this.timerName, function () {
		//do something...

		me.refreshNewMessage();//更新模式
	});


}


MessageGrid.onCheckDevice = function (simNo) {
	var zTree = $.fn.zTree.getZTreeObj("deviceTree");
	var node = zTree.getNodeByParam("simNo", simNo);
	var deviceId = node.id.substring(1, node.id.length);
	zTree.checkNode(node, true, true);//指定选中ID的节点
	var ztree1 = $.fn.zTree.getZTreeObj("deviceTree"), nodes = ztree1.getCheckedNodes(true);
	var selectDeviceIds = "";
	if (nodes && nodes.length) {
		for (var i = 0, len = nodes.length; i < len; i++) {
			var node = nodes[i];
			if (typeof (node.children) == "undefined" && node.id.indexOf("v") == 0) {
				nodeId = node.id.substring(1, node.id.length)
				selectDeviceIds += nodeId + ",";
			}
		}
		if (selectDeviceIds != "") {
			selectDeviceIds = selectDeviceIds.substring(0, selectDeviceIds.length - 1);
		}

		selectDeviceIds = selectDeviceIds;
		RealDataGrid.selectDeviceIds = selectDeviceIds;
	}
	var url = path + "/realData/registerDevicesBatch";
	var params = {registerDeviceIds: selectDeviceIds};
	$.ajax({
		type: "POST",
		url: url,
		data: params,
		error: function () {
			//alert("网络连接错误，无法读取数据!");
			//Utility.done();
		},
		success: function (data) {
			//刷新实时表格数据
			RealDataGrid.refresh(false);
		}
	});
	MyMap.showVehicleOnMap(deviceId);
}


//添加短信数据
MessageGrid.addNewMessage = function (messageData) {
	//最新的短信数据列表，数组
	var me = this;
	var soundEnabled = false;
	var needPopup = false;
	if (messageData.message && messageData.message.length > 0) {
		var messageArray = messageData.message;
		$.each(messageArray, function (i, message) {
			me.updateNotify(message); //弹出短信窗口
			needPopup = true;
			soundEnabled = true;
		});

		var lastMessage = messageArray[messageArray.length - 1];
		AlarmSound.play();//短信声音打开
	}

	//如果接收到,就弹窗
	if (needPopup)
		me.popNotifyWindow();//弹出窗口

}


MessageGrid.refreshNewMessage = function () {
	var url = path + "/message/message";
	var me = this;
	var params = {};
	$.ajax({
		type: "POST",
		url: url,
		data: params,
		error: function () {

		},
		success: function (data) {
			var result = data;
			if (result.success) {

				if (me.popNum > me.maxPopupNum) {
					me.closeWindow();//累计条数超过限制，清空避免占用内存
				}


				me.addNewMessage(result.data);
				//延时关闭窗口
				if (me.displayTime) {
					var now = new Date();
					var interval = now - me.displayTime;
					interval = 0.001 * interval;
					if (interval > 120) {
					//	me.closeWindow();
					}
				}

			} else {
				window.location = path + '/logout';
			}
		}
	});
}

var notifyWindow1;
MessageGrid.updateNotify = function (message) {
	this.popNum += 1;
	var messageContent = message.sendTime + ': ' + message.sendEquipName + ' 向 ' + message.recEquipName + ' <br/>发送内容:[' + message.message + ']';
	messageContent += "<br/>";
	$("#messageBox").prepend(messageContent);
}
MessageGrid.closeWindow = function () {
	this.popNum = 0;
	this.clearWindow();
	$('#messageWindow').window('close');
}


MessageGrid.popNotifyWindow = function (message) {
	if (notifyWindow1 == null) {
		notifyWindow1 = $('#messageWindow').window({
			title: '短信报文信息',
			width: 350,
			height: 200,
			top: ($(window).height() - 420),
			left: ($(window).width() - 350),
			shadow: false,
			modal: false,
			iconCls: 'fa fa-commenting-o',
			closed: true,
			minimizable: false,
			maximizable: false,
			collapsible: false,
			onBeforeClose:function(){
				this.popNum = 0;
				$("#messageBox").empty();
			}
		});
	}

	$('#messageWindow').window('open');
	this.displayTime = new Date();//更新窗口的显示时间，便于延迟加载
	if (message)
		this.updateNotify(message);

}

//清除短信窗口中的内容
MessageGrid.clearWindow = function () {
	$("#messageBox").empty();
	this.popNum = 0;
}

	

